package org.bdware.registry.webService.controller;

import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.bdware.doip.core.utils.DoipGson;
import org.bdware.doip.core.utils.GlobalConfigurations;
import org.bdware.irp.client.GlobalIrpClient;
import org.bdware.irp.exception.IrpClientException;
import org.bdware.registry.webService.model.ResponseWrapper;
import org.bdware.registry.webService.service.RegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 仓库管理模块
 */
@RestController
@RequestMapping("/doa/reg")
public class RegistryController {
    Logger logger = Logger.getLogger(RegistryController.class);

    @Autowired
    private RegistryService regService;

    @RequestMapping("")
    public String index() {
        List<String> indexStr = new ArrayList<>();
        indexStr.add("/getRegInfo");
        indexStr.add("/testRegConnection");
        indexStr.add("/connectRemoteReg");
        indexStr.add("/getInRegs");
        indexStr.add("/getOutRegs");
        indexStr.add("/getDOIPServiceInfo");
        indexStr.add("/getLRSInfo");
        return new Gson().toJson(indexStr);
    }

    /*
        url:http://127.0.0.1:8080/doa/reg/getreginfo
        返回JSon格式的DoipServiceInfo
     */
    @RequestMapping("/getRegInfo")
    public ResponseWrapper getRegInfo() {
        return new ResponseWrapper(regService.getRegInfo());
    }

    /*
        url:http://127.0.0.1:8080/doa/reg/getInRegs
        返回JSon格式的RegistryMap,接入的Registry
     */
    @RequestMapping("/getInRegs")
    public ResponseWrapper getInRegs() {
        return new ResponseWrapper(new Gson().toJson(regService.getInRegistry()));
    }

    /*
        url:http://127.0.0.1:8080/doa/reg/getOutRegs
        返回JSon格式的RegistryMap，接出的Registry
     */
    @RequestMapping("/getOutRegs")
    public ResponseWrapper getOutRegs() {
        return new ResponseWrapper(new Gson().toJson(regService.getOutRegistry()));
    }

    /*
        url:http://127.0.0.1:8080/doa/reg/testregconnection
        post，参数 serviceID
        返回JSon格式的 DoipServiceInfo
     */
    @RequestMapping("/getDOIPServiceInfo")
    public ResponseWrapper getDoipInfo(@RequestParam("serviceID") String serviceID) {
        try {
            return new ResponseWrapper(DoipGson.getDoipGson().toJson(GlobalIrpClient.getGlobalClient().resolve(serviceID)));
        } catch (IrpClientException e) {
            e.printStackTrace();
        }
        return new ResponseWrapper("{}");
    }

    /*
        url:http://127.0.0.1:8080/doa/reg/getLRSInfo
        返回LRS的URL
     */
    @RequestMapping("/getLRSInfo")
    public ResponseWrapper getLRSInfo() {
        return new ResponseWrapper(GlobalConfigurations.IRSAddress);
    }


    @RequestMapping("/connectRemoteReg")
    public ResponseWrapper connectRemoteReg(@RequestParam("regID") String regID, @RequestParam("level") String level) {
        try {
            int l = Integer.parseInt(level);
            return new ResponseWrapper(regService.addRemoteRegistry(regID,l));
        } catch (IrpClientException e) {
            e.printStackTrace();
        }
        return new ResponseWrapper(false);
    }


    @RequestMapping("/testRemoteReg")
    public ResponseWrapper testRemoteRegConnection(String regID){
        try {
            return new ResponseWrapper(regService.testRemoteRegConnection(regID));
        } catch (IrpClientException e) {
            e.printStackTrace();
        }
        return new ResponseWrapper(false);
    }

}
